var SmartChart = {
	okSign: false,
	option: {},
	cls: 'smartchart-preview',
	insertException: function(sign, msg, chartArea) {
		chartArea.children().remove();
		chartArea.addClass(sign);
		chartArea.append('<div style="text-align:center;line-height:400px">' + msg + '</div>');
		SmartChart.option = sign;
	},
	buildChart: function(optionData, status, target) {
		var emptyData;
		var chartArea;
		if (status == 'success') { // ajax
			chartArea = this.target;
			var chartType = this.chartType;
			emptyData = optionData.value.length == 0 ? true : false;
			if (emptyData) {
				SmartChart.option = 'empty_data';
				emptyData = 'empty_data';
			} else {
				if (chartType == 'line') {
					SmartChart.option = lineExtend(optionData);
				} else if (chartType == 'bar') {
					SmartChart.option = barExtend(optionData);
				}else if(chartType == 'doubleLine'){
					SmartChart.option = doubleLineExtend(optionData);
				}
			}

			if (chartArea.attr('id') != 'previewChart') {
				var chartParant = chartArea.parent();
				var id = chartParant.attr('id');
				chartParant.data(id, SmartChart.option);
			}
		} else if (status == 'insertChart') {
			chartArea = $(target);
			emptyData = optionData;
			SmartChart.option = optionData;
		}

		chartArea.attr('class', 'simplebox-content');
		chartArea.children().remove();
		if (emptyData == 'empty_data') {
			SmartChart.insertException('empty_data', '暂无数据', chartArea);
		} else if (emptyData == 'req_error') {
			SmartChart.insertException('req_error', '请求错误', chartArea);
		} else if (emptyData == 'config_error') {
			SmartChart.insertException('config_error', '配置错误', chartArea);
		} else {
			chart = echarts.init(chartArea[0]);
			try {
				chart.setOption(SmartChart.option);
			} catch (e) {
				SmartChart.insertException('config_error', '配置错误', chartArea);
			}
		}
	},
	ajaxReqCharData: function(code, xPoint, yPoint, yPoint2, targetEle, chartType) {
		var sendData = {
			pageId: pageId,
			code: code,
			xPoint: xPoint,
			yPoint: yPoint,
			yPoint2: yPoint2
		};
		ck.util.sendAjax({
			url: 'chart/getChartData',
			data: sendData,
			context: {
				target: targetEle,
				chartType: chartType
			}
		}, SmartChart.buildChart, function() {
			var chartArea = this.target;
			SmartChart.insertException('req_error', '请求错误', chartArea);
		});
	}
}
CKEDITOR.plugins.add('smartchart', {
	icons: 'smartchart',
	requires: 'widget',
	init: function(editor) {
		CKEDITOR.dialog.add('smartchart', this.path + 'dialogs/smartchart.js');

		editor.widgets.add('smartchart', {
			button: '插入chart图',
			dialog: 'smartchart',
			draggable: false,
			requiredContent: 'div(simplebox)',
			template: '<div class="simplebox ' + SmartChart.cls + '"><div class="simplebox-content" style="width:98.5%;height:400px"></div></div>',
			init: function() {
				this.once('ready', function() {
					if (this.data.isLoad == 1) {
						// 设置为0，避免 ctrl+z 会再次执行
						this.setData('isLoad', 0);

						var code = this.element.getAttribute('code');
						var xPoint = this.element.getAttribute('xpoint');
						var yPoint = this.element.getAttribute('ypoint');
						var yPoint2 = this.element.getAttribute('ypoint2');
						var id = this.element.getAttribute('id');
						var chartType = this.element.getAttribute('charttype');

						SmartChart.ajaxReqCharData(code, xPoint, yPoint, yPoint2, $(this.element.findOne('.simplebox-content').$), chartType);
						$(this.element.$).data(id, SmartChart.option);
					}
				})
			},
			upcast: function(el, data) {
				if (el.name == 'div' && el.hasClass(SmartChart.cls)) {
					// 标记此元素为页面加载的元素
					data.isLoad = 1;
					return el;
				}
			}
		});
	}
});